layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        table = layui.table,
        laydate = layui.laydate;

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'operation/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '操作记录列表',
        height: globalHeight,
        page: true,
        cols: [[
            {field: 'id', title: 'ID', width: 100, unresize: false, align: 'center'},
            {field: 'userId', title: '用户id', width: 130, unresize: false, align: 'center'},
            {field: 'username', title: '用户名', unresize: false, align: 'center'},
            {field: 'realname', title: '真实姓名', unresize: false, align: 'center'},
            {field: 'ip', title: 'ip', unresize: false, align: 'center'},
            {field: 'describe', title: '内容', unresize: false, align: 'center'},
            {field: 'createTime', title: '提交时间', unresize: false, align: 'center'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.datas //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'operation/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
        };
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var keyword = $('#keyword').val();
        var describe = $('#describe').val();
        var time = $('#time').val();

        return {'keyword': keyword, 'describe': describe, 'createTime': time};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#keyword').val(data.keyword);
        $('#describe').val(data.describe);
        $('#time').val(data.createTime);
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});